package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MUnitExcution;
import com.fortune.dto.in.mUnit.MUnitAddDto;
import com.fortune.dto.in.mUnit.MUnitEditDto;
import com.fortune.dto.in.mUnit.MUnitListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MUnitEnum;
import com.fortune.service.MUnitService;
import com.fortune.vo.mUnit.MUnit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "product", description = "商品单位管理", tags = {"商品单位管理相关"})
@RestController
@RequestMapping("mUnit")
public class MUnitController {
    @Autowired
    private MUnitService mUnitService;

    @ApiOperation(value = "商品单位列表", nickname = "mUnitList", notes = "" ,tags = {"@石匡"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
            @ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
            @ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
    })
    @GetMapping(value = "list",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData<MUnit>> list(HttpServletRequest request, @ApiIgnore @Validated MUnitListDto mUnitListDto) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        mUnitListDto.setPage((mUnitListDto.getPage()-1)*10);
        List<MUnit> list = mUnitService.queryMunit(companyId,mUnitListDto);
        int count = mUnitService.queryMunitCount(companyId,mUnitListDto);
        PagingData<MUnit> pagingData = new PagingData<>();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return new MUnitExcution<PagingData<MUnit>>(pagingData, MUnitEnum.SUCCESS);
    }

    @ApiOperation(value = "添加商品单位", nickname = "mUnitAdd", notes = "" ,tags = {"@石匡"})
    @PostMapping(value = "add",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody MUnitAddDto mUnitAddDto) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        int num  = mUnitService.queryMunitExist(companyId,mUnitAddDto);
        if (num > 0){
            return new MUnitExcution<Void>(null,MUnitEnum.EXIST);
        }
        num = mUnitService.addMunit(companyId,mUnitAddDto);
        if (num < 1){
            return new MUnitExcution<Void>(null,MUnitEnum.FAILURE);
        }
        return new MUnitExcution<Void>(null, MUnitEnum.SUCCESS);
    }

    @ApiOperation(value = "编辑商品单位", nickname = "mUnitEdit", notes = "" ,tags = {"@石匡"})
    @PutMapping(value = "edit",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody MUnitEditDto mUnitEditDto) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        int num  = mUnitService.queryEditMunitExist(companyId,mUnitEditDto);
        if (num > 0){
            return new MUnitExcution<Void>(null,MUnitEnum.EXIST);
        }
        num = mUnitService.editMunit(companyId,mUnitEditDto);
        if (num < 1){
            return new MUnitExcution<Void>(null,MUnitEnum.FAILURE);
        }
        return new MUnitExcution<Void>(null, MUnitEnum.SUCCESS);
    }

    @ApiOperation(value = "删除商品单位", nickname = "mUnitDelete", notes = "" ,tags = {"@石匡"})
    @ApiImplicitParam(name="mUnitId", value="商品单位id",paramType="query",dataType="string")
    @DeleteMapping(value = "delete",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotEmpty(message="商品单位id不能为空") String mUnitId) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        int num = mUnitService.deleteMunit(companyId,mUnitId);
        if (num < 1){
            return new MUnitExcution<Void>(null,MUnitEnum.FAILURE);
        }
        return new MUnitExcution<Void>(null, MUnitEnum.SUCCESS);
    }
}
